package com.ruoyi.develop.orderIronDetail.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.goodsCustomer.domain.GoodsCustomer;
import com.ruoyi.develop.orderIronRecord.domain.OrderIronRecord;
import com.ruoyi.develop.orderIronRecord.mapper.OrderIronRecordMapper;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import org.apache.poi.ss.usermodel.DateUtil;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReadStatus;
import org.jxls.reader.XLSReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.orderIronDetail.mapper.OrderIronDetailMapper;
import com.ruoyi.develop.orderIronDetail.domain.OrderIronDetail;
import com.ruoyi.develop.orderIronDetail.service.IOrderIronDetailService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 铁附件订单拆解明细Service业务层处理
 *
 * @author lmm
 * @date 2024-10-21
 */
@Service
public class OrderIronDetailServiceImpl implements IOrderIronDetailService {
    @Resource
    private OrderIronDetailMapper orderIronDetailMapper;

    @Resource
    private OrderIronRecordMapper orderIronRecordMapper;

    @Value(value = "${ruoyi.path}")
    public String basePath;
    @Value(value = "${ruoyi.templatePath}")
    public String templatePath;
    @Value(value = "${ruoyi.templateImport}")
    public String templateImport;

    /**
     * 查询铁附件订单拆解明细
     *
     * @param id 铁附件订单拆解明细主键
     * @return 铁附件订单拆解明细
     */
    @Override
    public OrderIronDetail selectById(String id) {
        return orderIronDetailMapper.selectById(id);
    }

    /**
     * 查询铁附件订单拆解明细列表
     *
     * @param entity 铁附件订单拆解明细
     * @return 铁附件订单拆解明细
     */
    @Override
    public List<OrderIronDetail> selectList(OrderIronDetail entity) {
        return orderIronDetailMapper.selectList(entity);
    }

    /**
     * 新增铁附件订单拆解明细
     *
     * @param entity 铁附件订单拆解明细
     * @return 结果
     */
    @Override
    public int insert(OrderIronDetail entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return orderIronDetailMapper.insert(entity);
    }

    /**
     * 修改铁附件订单拆解明细
     *
     * @param entity 铁附件订单拆解明细
     * @return 结果
     */
    @Override
    public int update(OrderIronDetail entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return orderIronDetailMapper.update(entity);
    }

    /**
     * 批量删除铁附件订单拆解明细
     *
     * @param ids 需要删除的铁附件订单拆解明细主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return orderIronDetailMapper.deleteByIds(ids);
    }

    /**
     * 删除铁附件订单拆解明细信息
     *
     * @param id 铁附件订单拆解明细主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return orderIronDetailMapper.deleteById(id);
    }

    @Override
    @Transactional
    public Map<String, Object> importData(OrderIronDetail detail, MultipartFile file) {
        String area = detail.getArea();
        area = area.substring(0, area.indexOf("?"));
        String tmplName = "orderIronDetail"+area+".xml";
        String errortext = "";
        String orderId = detail.getOrderId();
        String recordId = detail.getRecordId();
        Map<String, Object> map = new HashMap<>();
        int i = 0;
        int j = 0;
        List<OrderIronDetail> list = new ArrayList<>();
        List<OrderIronDetail> detailList = new ArrayList<>();
        try {
            // InputStream in = resource.getInputStream();
            InputStream in = new FileInputStream(basePath + File.separator
                    + templatePath + File.separator + templateImport + File.separator + tmplName);
            XLSReader mainReader = ReaderBuilder.buildFromXML(in);

            Map beans = new HashMap();
            beans.put("list", list);
            XLSReadStatus readStatus = mainReader.read(file.getInputStream(), beans);
            if (readStatus.isStatusOK()) {
                for (OrderIronDetail info : list) {

                    String unitWeight = info.getParams().get("unitWeight") != null ? info.getParams().get("unitWeight").toString() : "0";
                    String totalWeight = info.getParams().get("totalWeight") != null ? info.getParams().get("totalWeight").toString() : "0";
                    String unitPrice = info.getParams().get("unitPrice") != null ? info.getParams().get("unitPrice").toString() : "0";
                    String totalPrice = info.getParams().get("totalPrice") != null ? info.getParams().get("totalPrice").toString() : "0";
                    info.setUnitWeight(new BigDecimal(unitWeight));
                    info.setTotalWeight(new BigDecimal(totalWeight));
                    info.setUnitPrice(new BigDecimal(unitPrice));
                    info.setTotalPrice(new BigDecimal(totalPrice));

                    OrderIronDetail entity = new OrderIronDetail();
                    entity.setId(IdUtils.fastSimpleUUID());
                    BeanUtils.copyProperties(info, entity, "id");
                    detailList.add(entity);
                }
            }
            OrderIronRecord record = orderIronRecordMapper.selectById(recordId);

            BigDecimal totalWeight = new BigDecimal(0);
            if (StringUtils.isBlank(errortext)) {
                for (OrderIronDetail info : detailList) {

                    info.setOrderId(orderId);
                    info.setRecordId(recordId);
                    i += this.insert(info);
                    totalWeight = totalWeight.add(info.getTotalWeight());
                }
            }
            record.setWeight(totalWeight.add(record.getWeight()));
            orderIronRecordMapper.update(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("errorText", errortext);
        map.put("num", i);
        map.put("numupdate", j);
        return map;
    }

    @Override
    public void insertBatch(List<OrderIronDetail> insertList) {
        orderIronDetailMapper.insertBatch(insertList);
    }


}
